package gui;

import java.awt.Component;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.swing.GroupLayout;
import javax.swing.JPanel;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author jmoura
 */
public class CompDuplo extends JPanel implements Serializable {

    private static final Log log = LogFactory.getLog(CompDuplo.class);
    private Component compA;
    private Component compB;
    private ComponentFactory cf = new ComponentFactory();

    /**
     * Creates new form SubTabTextArea
     */
    public CompDuplo() {
        initComponents();
        initConfig();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jComp1 = new javax.swing.JComboBox();
        jComp2 = new javax.swing.JComboBox();

        jComp1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jComp2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jComp1, 0, 182, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jComp2, 0, 200, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jComp1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jComp2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox jComp1;
    private javax.swing.JComboBox jComp2;
    // End of variables declaration//GEN-END:variables

    private void initConfig() {
        compA = jComp1;
        compB = jComp2;
    }

    public void replaceA(Component newComponent) {
        GroupLayout parLayout = (GroupLayout) compA.getParent().getLayout();
        parLayout.replace(compA, newComponent);
        compA = newComponent;
    }

    public void replaceB(Component newComponent) {
        GroupLayout parLayout = (GroupLayout) compB.getParent().getLayout();
        parLayout.replace(compB, newComponent);
        compB = newComponent;
    }

    public String getParametrosId() {
        //formata string para comando. Separado por ; 
        List<String> parId = new ArrayList();
        List<String> parDisplay = new ArrayList();
        cf.getParametros(compA, parId, parDisplay);
        cf.getParametros(compB, parId, parDisplay);
        String ret = "";
        for (String par : parId) {
            ret += par + ";";
        }
        return ret;
    }

    public String[] getParametros() {
        //formata string para comando. Separado por ; 
        List<String> parId = new ArrayList();
        List<String> parDisplay = new ArrayList();
        cf.getParametros(compA, parId, parDisplay);
        cf.getParametros(compB, parId, parDisplay);
        String[] ret = {"", ""};
        for (String par : parDisplay) {
            ret[0] += ";" + par;
        }
        for (String par : parId) {
            ret[1] += ";" + par;
        }
        ret[0] = ret[0].substring(1).trim();
        ret[1] = ret[1].substring(1).trim();
        return ret;
    }
}
